package util;
/**
 * jdbc_util
 */

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
    private static  String password;
    private static  String user;
    private static  String url;

    public static void main(String[] args) {
        try {
            Connection conn = JDBCUtils.getConnection();
            System.out.println(conn);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //静态加载
    static{
        try {
        Properties pro = new Properties();
       ClassLoader classlord =JDBCUtils.class.getClassLoader();
      URL driver =  classlord.getResource("jdbc.properties");
            String path = driver.getPath();
            pro.load(new FileReader(path));
            password=pro.getProperty("password");
            user=pro.getProperty("user");
            url=pro.getProperty("url");
            Class.forName(pro.getProperty("driver"));
        } catch (ClassNotFoundException | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    public static void close(Connection con,Statement stat){

            try {if(con!=null){
                con.close();}
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //关闭资源
    public static void close(Connection con,Statement stat,ResultSet res){
        if(con!=null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(res!=null){
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}
